package com.apexwear.apexutils.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.apex.jieli.watcha.JieliWatchInfo;
import com.apexwear.apexutils.R;

import java.util.ArrayList;
import java.util.List;

public class AdapterDial extends RecyclerView.Adapter<AdapterDial.DialHold> {
    List<JieliWatchInfo> infos;

    public AdapterDial(List<JieliWatchInfo> infos) {
        this.infos = infos;
    }

    @NonNull
    @Override
    public DialHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View childView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_dial_list, parent, false);
        return new DialHold(childView);
    }

    @Override
    public void onBindViewHolder(@NonNull DialHold holder, int position) {
        if (infos == null || infos.isEmpty()) {
            return;
        }
        final JieliWatchInfo jieliWatchInfo = infos.get(position);
        if (jieliWatchInfo == null) {
            return;
        }
        holder.imageView.setOnCheckedChangeListener(null);
        if (jieliWatchInfo.size == 2) {
            holder.imageView.setChecked(true);
        } else {
            holder.imageView.setChecked(false);
        }

        holder.textView.setText(jieliWatchInfo.name);
        holder.imageView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    jieliWatchInfo.size = 2;
                } else {
                    jieliWatchInfo.size = 1;
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return infos == null ? 0 : infos.size();
    }

    class DialHold extends RecyclerView.ViewHolder {
        TextView textView;
        CheckBox imageView;

        public DialHold(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.cycle_txt);
            imageView = itemView.findViewById(R.id.check);
        }
    }

    public List<JieliWatchInfo> getDeleteWatchFace() {
        if (infos == null) {
            return null;
        }
        List<JieliWatchInfo> jieliList = new ArrayList<>();
        for (JieliWatchInfo info : infos) {
            if (info != null && info.size == 2) {
                jieliList.add(info);
            }
        }
        return jieliList;
    }
}
